import copy
from typing import List


class Solution:
    def countValidSelections(self, nums: List[int]) -> int:
        n = len(nums)
        def dfs(dir,idx,arr):
            while 0 <= idx < n:

                if arr[idx] > 0:
                    arr[idx] -= 1
                    dir = -dir
                idx += dir
            return sum(arr) == 0
        ans = 0
        for i in range(n):
            if nums[i] == 0:
                for dir in [-1,1]:
                    ans += dfs(dir,i,copy.copy(nums))
        return ans

if __name__ == '__main__':
    s = Solution()
    r = s.countValidSelections([1,0,2,0,3])
    print(r)